package com.ruoyi.cp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.cp.domain.Lottery;
import com.ruoyi.cp.domain.VipLog;
import com.ruoyi.cp.mapper.LotteryMapper;
import com.ruoyi.cp.mapper.VipLogMapper;
import com.ruoyi.cp.service.ILotteryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 彩票用户 服务层实现
 *
 * @author wh
 * @date 2019-06-25
 */
@Slf4j
@Service
public class LotteryServiceImpl extends ServiceImpl<LotteryMapper, Lottery> implements ILotteryService {
    @Autowired
    LotteryMapper lotteryMapper;

    @Autowired
    VipLogMapper vipLogMapper;

    @Override
    public List<Lottery> list(Lottery lottery) {
        LambdaQueryWrapper<Lottery> wrapper = new LambdaQueryWrapper<>();
        if (lottery != null) {
            if (StringUtils.isNotEmpty(lottery.getLotteryId())) {
                wrapper.eq(Lottery::getLotteryId, lottery.getLotteryId());
            }
            if (StringUtils.isNotEmpty(lottery.getPhone())) {
                wrapper.eq(Lottery::getPhone, lottery.getPhone());
            }
            if (StringUtils.isNotEmpty(lottery.getUserName())) {
                wrapper.like(Lottery::getUserName, lottery.getUserName());
            }
            if (StringUtils.isNotEmpty(lottery.getIsVip())) {
                wrapper.eq(Lottery::getIsVip, lottery.getIsVip());
            }
            if (StringUtils.isNotEmpty(lottery.getVipExpire())) {
                wrapper.eq(Lottery::getVipExpire, lottery.getVipExpire());
            }
            if (StringUtils.isNotEmpty(lottery.getCreateTime())) {
                wrapper.eq(Lottery::getCreateTime, lottery.getCreateTime());
            }

            Object beginTime = lottery.getParams().get("beginTime");
            Object endTime = lottery.getParams().get("endTime");
            if (StringUtils.isNotEmpty(beginTime)) {
                wrapper.ge(Lottery::getCreateTime, beginTime);
            }
            if (StringUtils.isNotEmpty(endTime)) {
                wrapper.le(Lottery::getCreateTime, endTime);
            }
        }
        return baseMapper.selectList(wrapper);
    }

    /**
     * 开通会员
     *
     * @param lotteryId 用户id
     * @param vipExpire vip到期时间
     * @throws CustomException
     */
    @Override
    @Transactional(rollbackFor = {CustomException.class})
    public void addVip(Integer lotteryId, String vipExpire) throws CustomException {
        Lottery lottery = lotteryMapper.selectById(lotteryId);
        Date date = DateUtils.parseDate(vipExpire);
        lottery.setVipExpire(date);
        lottery.setIsVip(1);
        lotteryMapper.updateById(lottery);

        // 添加开通记录
        VipLog vipLog = new VipLog();
        vipLog.setLotteryId(lotteryId);
        vipLog.setOpenTime(new Date());
        vipLog.setExpireTime(date);
        vipLogMapper.insert(vipLog);
    }

    public static void main(String[] args) {
        Date date = DateUtils.parseDate("2019-06-25");
        System.out.println(date);
    }
}
